<?php
/**
 *   Joomdiet - administrator/components/models/variable.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
class JoomdietModelVariable extends JModel
{
    var $_data;
    var $_pagination = null;
    var $_total = null;
    
    function __construct()
    {
        parent::__construct();
        
        global $mainframe, $option;
        
        $limit = $mainframe->getUserStateFromRequest('global.list.limit',
                                                     'limit',
                                                     $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
        
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
        
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    function setId($id)
    {
        $this->_id = $id;
        $this->_data = null;
    }
    
    // default query constructor
    function _buildQuery() {
        $query = 'SELECT * FROM #__joomdiet_Variable';
        return $query;
    }
    
    function getPagination()
    {
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(),
                                                 $this->getState('limitstart'),
                                                 $this->getState('limit'));
        }
        return $this->_pagination;
    }
    
    function getData()
    {
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }
        
        return $this->_data;
    }
    
    function getAllData()
    {
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query);
        }
        
        return $this->_data;
    }
    
    function getModel()
    {
        return $this;
    }
    
    function getVariable($id)
    {
        $query = 'SELECT * FROM #__joomdiet_Variable WHERE id='.$id;
        $this->_db->setQuery($query);
        return $this->_db->loadObject();
    }
    
    function getVariableByName($name)
    {
        $query = 'SELECT * FROM #__joomdiet_Variable WHERE name='.$name;
        $this->_db->setQuery($query);
        $object = $this->_db->loadObject();
        return $object;
    }
    
    function getTotal()
    {
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }
        return $this->_total;
    }
    
    function delete($cid)
    {
        if (!$cid) return false;
        else {
            $table = $this->getTable();
            
            if (!$table->delete($cid)) {
                return false;
            }
            
            return true;
        }
    }
    
    function exists($id) {
        $query = 'SELECT count(*) FROM #__joomdiet_Variable WHERE id='.$id;
        $this->_db->setQuery($query);
        return $this->_db->loadResult();
    }
    
    function update($variable) {
        $query = 'UPDATE #__joomdiet_Variable SET name="'.$variable->name.'"';
        $query .= ', description="'.$variable->description.'"';
        $query .= ', unit="'.$variable->unit.'"';
        $query .= ', min_value="'.$variable->min_value.'"';
        $query .= ', max_value="'.$variable->max_value.'"';
        $query .= ', category="'.$variable->category.'"';
        // $query .= ', suggested_amount_formula="'.$variable->suggested_amount_formula.'"';
        $query .= ' WHERE id='.$variable->id;
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function add($variable) {
        $query = 'INSERT INTO #__joomdiet_Variable (name, description, unit,';
        $query .= ' min_value, max_value, category) VALUES (';
        $query .= '"'.$variable->name.'", "'.$variable->description.'", ';
        $query .= '"'.$variable->unit.'", "'.$variable->min_value.'", ';
        $query .= '"'.$variable->max_value.'", "'.$variable->category.'")';
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function getNames() {
        $query = 'SELECT name FROM #__joomdiet_Variable';
        $this->_db->setQuery($query);
        return $this->_db->loadResultArray();
    }
}
?>
